﻿using System;
using System.Collections;
using System.Text;
using Castle.ActiveRecord;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Common
{
    /// <summary>
    /// 方便设置或获取ActiveRecordValidationBase中的函数或属性
    /// </summary>
    public class ActiveRecordValidationBaseHelper
    {
        /// <summary>
        /// Gets the error MSG.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static string GetErrorMsg(ActiveRecordValidationBase model)
        {
            string msg = "";

            IEnumerable errorMsgValidation = model.ValidationErrorMessages;
            foreach (string item in errorMsgValidation)
            {
                msg += item + "\r\n";
            }
            return msg;
        }
        /// <summary>
        /// Gets the error MSG.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static string GetErrorMsg(ActiveRecordValidationBase model, Control page, Control webControl)
        {
            string msg = "";

            IEnumerable errorMsgValidation = model.ValidationErrorMessages;
            foreach (string item in errorMsgValidation)
            {
                webControl = webControl ?? FindControl(page, webControl, item, model);
                msg += item + "\r\n";
            }
            return msg;
        }

        private static Control FindControl(Control page, Control webControl, string item, ActiveRecordValidationBase model)
        {
            return webControl;
        }
    }
}
